package com.hsogoo.collate.batch;

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

import java.lang.reflect.Field;

/**
 * 动态设置字段属性映射
 * @Author huangsaigang
 * Created @2019-10-29.
 */
public class CollateFieldSetMapper implements FieldSetMapper<CollateFields> {

    @Override
    public CollateFields mapFieldSet(FieldSet fieldSet) throws BindException {

        CollateFields collateFields = new CollateFields();
        for(String configField : fieldSet.getNames()){
            try {
                Field field = collateFields.getClass().getDeclaredField(configField);
                field.setAccessible(true);
                field.set(collateFields, fieldSet.readString(configField));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //Clear
        return collateFields;
    }

}
